package spring.cloud.service.task.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import spring.cloud.service.task.analyzer.StatisticsRefreshService;
import spring.cloud.service.task.domain.different_info.TaskDifferentInfo;
import spring.cloud.service.task.domain.zooKeeperAndNamespace.ZooKeeper;
import spring.cloud.service.task.dto.NamespaceDto;
import spring.cloud.service.task.dto.ZooKeeperDto;
import spring.cloud.service.task.mapper.NameSpaceDtoMapper;
import spring.cloud.service.task.mapper.ZooKeeperDtoMapper;
import spring.cloud.service.task.service.TaskService;
import spring.cloud.service.task.service.ZooKeeperDatabaseTaskDifferentInfoService;
import spring.cloud.service.task.service.ZooKeeperService;
import spring.cloud.service.task.utils.Result;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/api")
public class UtilsController {
    @Autowired
    private NameSpaceDtoMapper nameSpaceDtoMapper;

    @Resource
    private TaskService taskService;
    @Resource
    private ZooKeeperService zooKeeperService;
    @Resource
    private ZooKeeperDtoMapper zooKeeperDtoMapper;

    @Resource
    private StatisticsRefreshService statisticsRefreshService;
    @Resource
    private ZooKeeperDatabaseTaskDifferentInfoService zooKeeperDatabaseTaskDifferentInfoService;

    @RequestMapping(value = "/getZooKeeperClusterList")
    public Result getZooKeeperClusterList() {
        Integer page = null;
        Integer limit = null;
        List<ZooKeeper> zooKeeperServiceZooKeeperClusterList = new ArrayList<>();
        List<String> list = new ArrayList<String>();
        for (ZooKeeper zooKeeperCluster : zooKeeperServiceZooKeeperClusterList) {
            list.add(zooKeeperCluster.getZooKeeperKey());
        }
        return Result.success(list);
    }

    /**
     * 发现nameSpace相关注册信息.
     */

    @RequestMapping(value = "/getConnectString", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity discover(String namespaceKey) throws RuntimeException {
        Map<String, String> response = new HashMap<>(2);
        response.put("connectString", getConnectString(namespaceKey));
        return ResponseEntity.ok(response);
    }

    /**
     * 返回ZK连接串
     */
    private String getConnectString(String namespaceKey) throws RuntimeException {
        NamespaceDto namespaceDto = nameSpaceDtoMapper.selectByNamespaceKey(namespaceKey);
        if (namespaceDto == null) {
            throw new RuntimeException("命名空间:" + namespaceKey + "不存在");
        }
        String zooKeeperKey = namespaceDto.getZookeeperKey();
        ZooKeeperDto zooKeeperDto = zooKeeperDtoMapper.selectByZooKeeperKey(zooKeeperKey);
        if (zooKeeperDto == null) {
            throw new RuntimeException("连接信息:" + zooKeeperKey + "不存在");
        }
        return zooKeeperDto.getAddressList();
    }

    /**
     * 根据ZK集群key，刷新该集群的dashboard信息
     */
    //@Audit(type = AuditType.REST)
    @RequestMapping(value = "/statistics/refresh", method = {RequestMethod.POST, RequestMethod.GET}, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity<Object> dashboardRefresh(String zooKeeperClusterKey, HttpServletRequest request) throws RuntimeException {
        try {

            long beforeRefresh = System.currentTimeMillis();
            statisticsRefreshService.refresh(zooKeeperClusterKey);
            long afterRefresh = System.currentTimeMillis();
            long takeTime = afterRefresh - beforeRefresh;
            return new ResponseEntity<Object>(takeTime, HttpStatus.OK);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }


    @RequestMapping(value = "/differentByZooKeeperCluster", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public ResponseEntity<Object> differentByZooKeeperCluster(String zooKeeperClusterKey, HttpServletRequest request)
            throws RuntimeException {
        try {
            List<TaskDifferentInfo> differentInfoList = zooKeeperDatabaseTaskDifferentInfoService.differentByZooKeeperClusterKey(zooKeeperClusterKey);
            return new ResponseEntity<Object>(differentInfoList, HttpStatus.OK);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

}
